home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Sort Function
- Date: 31 Mar 1996 22:39:59 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4jn1jv$1m9@sparcserver.lrz-muenchen.de>
- References: <4jmq99$cqi@freenet-news.carleton.ca>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- aq436@FreeNet.Carleton.CA (Jerry Boyd) writes:
-
- >How would I WRITE a function (called sort3) that would
- >sort three integers (in assending order) by using pointers
- >and NOT arrays?
-
- How about:
-
- #include <stdlib.h>
-
- static int
- s3cmp(const void *a, const void *b)
- {
- const int ia = *(const int *)a;
- const int ib = *(const int *)b;
-
- return ia < ib ? - 1 : ia > ib ? 1 : 0;
- }
-
- void
- sort3(int *a, int *b, int *c)
- {
- int tmp[3], *p;
-
- *tmp = *a;
- p = tmp;
- *(++p) = *b;
- *(++p) = *c;
- qsort(tmp, sizeof tmp / sizeof *tmp, sizeof *p, s3cmp);
- *c = *p--;
- *b = *p--;
- *a = *p;
- }
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-